﻿//////////////////////////////////////////////////////////////////
//
// Hester\Toons.cs
//
// Copyright (c) 2012 Dan Pike.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation files
// (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sub-license, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using DanPike.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hester
{
   public class Toons
   {
      private static Logger Logger = Program.Logger;

      /// <summary>
      /// Maps a Tuple<serverName, toonName> to a Toon instance
      /// </summary>
      private Dictionary<Tuple<string, string>, Toon> toons_ = new Dictionary<Tuple<string, string>, Toon>();

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// 
      /// </summary>
      public Toons()
      {
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// 
      /// </summary>
      /// <param name="account"></param>
      /// <param name="server"></param>
      /// <param name="toonName"></param>
      /// <returns></returns>
      public Toon addToon(Account account, Server server, string toonName)
      {
         try
         {
            // Toon names are unique to a given server
            Tuple<string, string> toonId = new Tuple<string, string>(server.Name, toonName);
            Toon toon = null;
            if (!toons_.TryGetValue(toonId, out toon))
            {
               toon = new Toon(account, server, toonName);
               toons_.Add(toonId, toon);
            }
            return toon;
         }
         catch (System.Exception ex)
         {
            Logger.Exception(ex);
         }
         return null;
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// 
      /// </summary>
      /// <param name="serverName"></param>
      /// <returns></returns>
      public List<Toon> getServerToons(string accountName, string serverName)
      {
         try
         {
            return toons_.Where(kvp1 =>
                              (kvp1.Key.Item1 == serverName) &&
                              (kvp1.Value.Account.Name == accountName))
                         .Select(kvp2 => kvp2.Value)
                         .ToList();
         }
         catch (System.Exception ex)
         {
            Logger.Exception(ex);
         }
         return null;
      }
   }
}
